package com.ruoyi.web.controller.app.service;

import com.ruoyi.framework.web.exception.base.XException;
import com.ruoyi.sj.domain.Money;
import com.ruoyi.sj.domain.Withdraw;
import com.ruoyi.sj.mapper.MoneyMapper;
import com.ruoyi.sj.mapper.WithdrawMapper;
import com.ruoyi.sj.service.impl.WithdrawServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class AppChangeService {
    @Autowired
    private WithdrawMapper withdrawMapper;
    @Autowired
    private MoneyMapper moneyMapper;

    /**
     * 提现
     * @param memerId
     * @param bankCardNum
     * @param money
     * @return
     */
    public Object withDraw(Long memerId, String bankCardNum, Double money) {

        Double remainingMonry = moneyMapper.selectMoneyByMemberId(memerId);
        if (remainingMonry < money) {
            throw new XException("余额不足");
        } else {
            Withdraw withdrawDomain = new Withdraw();
            //银行卡号
            withdrawDomain.setBankCardNum(bankCardNum);
            //memberID
            withdrawDomain.setMemberId(memerId);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-mm-dd hh:mm:ss");
            String format = simpleDateFormat.format(new Date());
            //提现时间
            withdrawDomain.setWithdrawTime(format);
            //提现状态
            withdrawDomain.setStatus(3);
            //设置提现金额
            withdrawDomain.setWithdrawMoney(BigDecimal.valueOf(money));
            int i = withdrawMapper.insertWithdraw(withdrawDomain);
            return i;
        }
    }
}
